<template>
  <el-dialog
    title="单位信息"
    ref="tableDialog"
    :visible="dialogVisible"
    width="80%"
    v-if="dialogVisible"
    @close="close"
  >
    <h4 class="s_title">基本信息</h4>
    <el-form :model="formData" label-width="120px" :rules="formRules" ref="form">
      <el-row>
        <el-col :sm="8">
          <el-form-item label="单位名称" prop="companyName">
            <el-input v-model="formData.companyName" placeholder="请输入单位名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="8">
          <el-form-item label="拼音缩写" prop="pinYin">
            <el-input v-model="formData.pinYin" placeholder="请输入拼音缩写"></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="8" v-if="companyId">
          <el-form-item label="单位编码" prop="companyCode">
            <el-input v-model="formData.companyCode" :disabled="true" placeholder="请输入单位编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :sm="8">
          <el-form-item label="隶属类别">
            <el-input value="省直部门及所属单位" :disabled="true" placeholder="请输入隶属类别"></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="8">
          <el-form-item label="隶属类型">
            <el-select
              v-model="formData.tagId"
              :disabled="Boolean(companyId)||usertype==3"
              placeholder="请输入隶属类型"
              style="width: 100%;"
              @change="tagchange"
            >
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in tags"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :sm="8">
          <el-form-item label="上级单位">
            <el-select v-model="formData.parentId" placeholder="请选择上级单位" style="width: 100%;">
              <el-option
                :label="item.companyName"
                :value="item.id"
                v-for="(item, index) in parent"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>-->
        <el-col :sm="8">
          <el-form-item label="上级单位">
            <el-select
              v-model="formData.parentId"
              clearable
              filterable
              placeholder="请选择上级单位"
              style="width: 100%;"
            >
              <el-option
                :label="item.companyName"
                :value="item.id"
                v-for="(item, index) in parent"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :sm="8">
          <el-form-item label="单位规格" prop="standardId">
            <el-select v-model="formData.standardId" placeholder="请选择单位规格" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in standards"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="8">
          <el-form-item label="单位性质" prop="properId">
            <el-select v-model="formData.properId" placeholder="请选择单位性质" style="width: 100%;">
              <el-option
                :label="item.name"
                :value="item.id"
                v-for="(item, index) in propers"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" style="text-align: left;">
      <el-button type="primary" @click="save">确定</el-button>
      <el-button @click="resetForm">重置表单</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { axios } from "@/api/axios";
import { mapGetters } from "vuex";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: true,
    },
    companyId: {
      type: Number,
      default: null,
    },
    level: {
      type: String,
      default: null,
    },
  },
  data: function () {
    return {
      formData: {},
      tags: [],
      parent: [],
      usertype: 0, //user权限
      propers: [], //单位性质
      standards: [], //单位规格
      //表单验证
      formRules: {
        companyName: [
          {
            required: true,
            message: "请输入单位名称",
            trigger: "blur",
          },
        ],
        pinYin: [
          {
            required: true,
            message: "请输入拼音缩写",
            trigger: "blur",
          },
        ],
        standardId: [
          {
            required: true,
            message: "请选择单位规格",
            trigger: "blur",
          },
        ],
        properId: [
          {
            required: true,

            message: "请选择单位性质",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.fetchData();
    this.usertype = this.userInfo.usertype;
    console.log(this.usertype);
  },
  methods: {
    fetchData() {
      var param = {
        level: this.level,
      };
      if (this.companyId) {
        param = {
          companyId: this.companyId,
          level: this.level,
        };
      }
      axios("/admin/company/showDetails2.do", "get", param).then((data) => {
        if (data.error == 1) {
          this.$message.error("系统出错");
        } else {
          if (this.companyId) {
            if (data.company.parentId == null) {
              data.company.parentId = -1;
            }
            this.formData = {
              companyName: data.company.companyName,
              companyCode: data.company.companyCode,
              pinYin: data.company.pinYin,
              subLevId: data.company.subLevId,
              tagId: data.company.tagId,
              parentId: data.company.parentId,
              standardId: data.company.standardId,
              properId: data.company.properId,
              id: data.company.id,
            };
            this.parent = data.parentCompanys;
          } else {
            if (this.usertype == 3) {
              this.formData = {
                companyName: "",
                pinYin: "",
                subLevId: 1,
                tagId: this.userInfo.tagId * 1,
                parentId: "",
              };
            } else {
              this.formData = {
                companyName: "",
                pinYin: "",
                subLevId: 1,
                tagId: "",
                parentId: "",
              };
            }
            this.parent = data.parent;
          }
          this.propers = data.propers;
          this.standards = data.standards;
          this.tags = data.tags;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    tagchange() {
      if (this.companyId) {
      } else {
        var param = {
          level: this.level,
          tagId: this.formData.tagId,
        };
        axios("/admin/company/showDetails2.do", "get", param).then((data) => {
          if (data.error == 1) {
            this.$message.error("系统出错");
          } else {
            this.parent = data.parent;
          }
        });
      }
    },
    save() {
      var formData = JSON.parse(JSON.stringify(this.formData));
      formData["parentComId"] = formData.parentId;
      var param = new URLSearchParams();
      for (var key in formData) {
        if (formData[key] instanceof Array) {
          param.append(key, formData[key].join(","));
        } else {
          param.append(
            key,
            typeof formData[key] === "undefined" ? "" : formData[key]
          );
        }
      }
      var url = "/admin/company/addCompany.do";
      if (this.companyId) {
        url = "/admin/company/changeCompany1.do";
      }
      this.$refs.form.validate((pass) => {
        if (!pass) {
          return;
        }
        axios(url, "post", param).then((data) => {
          if (data.error == 0) {
            if (url == "/admin/company/addCompany.do") {
              this.$alert(
                "账号：" + data.uLoginName + "<br />" + "密码：" + data.uPwd,
                "管理员信息",
                {
                  dangerouslyUseHTMLString: true,
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.$message.success("保存成功");
                    this.$emit("update:dialogVisible", false);
                    this.$emit("finish");
                    this.$store.dispatch("user/getLeftJson");
                  },
                }
              );
            } else {
              this.$message.success("保存成功");
              this.$emit("update:dialogVisible", false);
              this.$emit("finish");
              this.$store.dispatch("user/getLeftJson");
            }
          } else {
            this.$message.error("保存失败");
          }
        });
      });
    },
    close() {
      this.$emit("update:dialogVisible", false);
    },
  },
};
</script>

<style lang="scss" scoped>
.s_title {
  background: #f6f6f6;
  height: 40px;
  line-height: 40px;
  padding-left: 10px;
  margin-top: 0;
}
</style>